<?php
namespace Admin\Controller;
class FuturesDrawController extends AdminController
{
	/**
	* 提现列表
	* 
	* @return
	*/
	public function index(){
		$model=M('futures_withdrawals');
		//状态搜索
		if($_REQUEST['is_pay']){
            if($_REQUEST['is_pay']==1){
                $map['is_pay']=array('eq',1);
                $is_pay=1;
            }elseif($_REQUEST['is_pay']==2){
                $map['is_pay']=array('eq',2);
                $is_pay=2;
            }elseif($_REQUEST['is_pay']==3){
            	$map['is_pay']=array('eq',0);
            	$is_pay=3;
			}else{
				$is_pay=-2;
			}
        }
        else{
             $is_pay=-2;
       }
       $this->assign('is_pay',$is_pay);
       
        //持卡人搜索
	
         if($_REQUEST['uname']){
		 	$bank_realname=$_REQUEST['uname'];
         	$map['b.nickname']=array('like',"%".trim($bank_realname)."%");
         	$this->assign('bank_realname',$bank_realname,'','trim');
		 }
		if($_REQUEST['mobile']){
 		 		$mobile=I('mobile');
				$map['b.mobile']=array('like',"%".trim($mobile)."%");
				$this->assign('mobel',$mobile,'','trim');
 		 }
		 //身份证搜索
         if($_REQUEST['id_card']){
		 	$id_card=$_REQUEST['id_card'];
         	$map['id_card']=array('like',"%".trim($id_card)."%");
         	$this->assign('id_card',$id_card,'','trim');
		 }
		 //卡号搜索
         if($_REQUEST['bank_card']){
		 	$bank_card=$_REQUEST['bank_card'];
         	$map['bank_card']=array('like',"%".trim($bank_card)."%");
         	$this->assign('bank_card',$bank_card,'','trim');
		 }
       	$start_time=I('get.start_time');
		if($start_time){
			$map['a.add_time']=array('egt',strtotime($start_time));
			$this->assign('start_time',$start_time);
		}
		$end_time=I('get.end_time');
		if($end_time){
			$end_time=strtotime($end_time)+86400;
			$map['_string']='a.add_time <='.$end_time;
		}
       //end 搜索
       if( isset($REQUEST['r']) ){
            $listRows = (int)$REQUEST['r'];
        }else{
            $listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 20;
        }
        $count_list=$model->join('wp_follow as b ON wp_futures_withdrawals.user_id = b.id')->where($map)->field('count(wp_futures_withdrawals.id) as count')->find();
        $Page       = new \Think\Page($count_list['count'],$listRows);
       	$show       = $Page->show();
  		$list=$model->table('wp_futures_withdrawals as a')
  		->join('wp_follow as b ON a.user_id = b.id')
  		->join('wp_futures_user_bank as c ON a.bank_id = c.id')
  		->field('a.*,c.pro_name,c.city_name,c.id_card,c.bank_card,c.bank_realname,c.bank_name,c.outlets_name,b.mobile')
  		->where($map)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
  		foreach($list as $k=>$v){
  			$follow_where['id']=$v['user_id'];
			$list[$k]['nickname']=M('follow')->where($follow_where)->getField('nickname');
			$list[$k]['blocked_fund']=M('follow')->where($follow_where)->getField('blocked_fund');
		}
		$moneydata=$model->table('wp_futures_withdrawals as a')
				->join('wp_follow as b ON a.user_id = b.id')
				->join('wp_futures_user_bank as c ON a.bank_id = c.id')
				->field('a.*,c.pro_name,c.city_name,c.id_card,c.bank_card,c.bank_realname,c.bank_name,c.outlets_name,sum(a.money) as tixian,sum(a.handling_fee) as shouxu')
				->where($map)->select();
		foreach($moneydata as $vv){
			$tixian=$vv['tixian'];
			$shouxu=$vv['shouxu'];
		}
		$tixian = round($tixian,2);
		$shouxu = round($shouxu,2);
		$this->assign('tixian',$tixian);
		$this->assign('shouxu',$shouxu);
       $this->assign('list',$list);// 赋值数据集
       $this->assign('page',$show);// 赋值分页输出
       $this->display();
	}
	/**
	* 
	* 确认提现操作
	* @return
	*/
	public function draw_status(){
		$id=I('get.id');
		$where['id']=array('eq',$id);
		$money=M('futures_withdrawals')->where($where)->getField('money');				//获取金额
		$handling_fee = M('futures_withdrawals')->where($where)->getField('handling_fee');//获取手续费
		$user = M('futures_withdrawals')->where($where)->getField('user_id');			//获取用户ID
		$blocked_fund = M('follow')->where('id='.$user)->getField('blocked_fund');		//获取冻结资金
		//确认后的金额
		$follow['blocked_fund'] = $blocked_fund-$money-$handling_fee;
		$data['pay_time'] = time();				//确认时间
		$data['is_pay']=1;						//修改状态
		$data['remark']="提现金额".$money."元";		//说明
		if(M('futures_withdrawals')->where($where)->save($data)===false){
			$this->error('确认失败');
		}else{
			M('follow')->where('id='.$user)->save($follow);		//修改冻结资金
			$this->success('确认成功');
		}
	}
	/**
	 *
	 * 退回提现操作
	 * @return
	 */
	public function draw_return(){
		$id=I('get.id');
		$where['id']=array('eq',$id);
		$list=M('futures_withdrawals')->where($where)->find();
		$money=$list['money'];		//获取金额
		$handling_fee = $list['handling_fee'];//获取手续费
		$con_money = $money+$handling_fee;//退回总额
		$uid=$list['user_id'];		//获取用户ID
		$follow_where['id']=array('eq',$uid);
		$blocked_fund = M('follow')->where('id='.$uid)->getField('blocked_fund');				//获取冻结资金
		$openid=M('follow')->where($follow_where)->getField('openid');							//获取用户openid
		//退回后的金额
		$follow['blocked_fund'] = $blocked_fund-$con_money;
		//交易记录
		$note="退回金额".$con_money."到用户余额";				//记录说明
		modify_account($openid,$money+$handling_fee,8,$note);
		//修改提现状态、说明、退款时间
		$data['pay_time'] = time();
		$data['is_pay']=2;
		$data['remark']="退回金额".$con_money."到用户余额";	//说明
		if(M('futures_withdrawals')->where($where)->save($data)===false){
			$this->error('退款失败');
		}else{
			M('follow')->where('id='.$uid)->save($follow);		//修改冻结资金
			$this->success('退款成功');
		}	
	}
	/**提现列表导出
	 */
	public function download(){
		$model=M('futures_withdrawals');
        //状态搜索
		if($_REQUEST['is_pay']){
            if($_REQUEST['is_pay']==1){
                $map['is_pay']=array('eq',1);
                $is_pay=1;
            }elseif($_REQUEST['is_pay']==2){
                $map['is_pay']=array('eq',2);
                $is_pay=2;
            }elseif($_REQUEST['is_pay']==3){
            	$map['is_pay']=array('eq',0);
            	$is_pay=3;
			}else{
				$is_pay=-2;
			}
        }
        else{
             $is_pay=-2;
       }
       $this->assign('is_pay',$is_pay);
       //持卡人搜索
         if($_REQUEST['bank_realname']){
		 	$bank_realname=$_REQUEST['bank_realname'];
         	$map['bank_realname']=array('like',"%".trim($bank_realname)."%");
         	$this->assign('bank_realname',$bank_realname,'','trim');
		 }
		 //身份证搜索
         if($_REQUEST['id_card']){
		 	$id_card=$_REQUEST['id_card'];
         	$map['id_card']=array('like',"%".trim($id_card)."%");
         	$this->assign('id_card',$id_card,'','trim');
		 }
		 //卡号搜索
         if($_REQUEST['bank_card']){
		 	$bank_card=$_REQUEST['bank_card'];
         	$map['bank_card']=array('like',"%".trim($bank_card)."%");
         	$this->assign('bank_card',$bank_card,'','trim');
		 }
       if ($_REQUEST['add_time']) {//交易时间
            $time=strtotime($_REQUEST['add_time']);
            $start_time=mktime(00,00,00,date('m',$time),date('d',$time),date('Y',$time));
            $end_time=mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
            $map['a.add_time'] = array('between',"$start_time,$end_time");
             $this->assign('add_time',$_REQUEST['add_time']);
        }
  		$list=$model->table('wp_futures_withdrawals as a')->join('wp_futures_user_bank as b ON a.bank_id = b.id')->field('a.*,b.pro_name,b.city_name,b.id_card,b.bank_card,b.bank_realname,b.bank_name,b.outlets_name')->where($map)->select();
  		foreach($list as $k=>$v){
  			$follow_where['id']=$v['user_id'];
			$list[$k]['nickname']=M('follow')->where($follow_where)->getField('nickname');
			$list[$k]['blocked_fund']=M('follow')->where($follow_where)->getField('blocked_fund');
		}
		$twotime=date("Y-m-d H:i:s");
        $filename= $twotime.'提现信息.csv';
        header("Content-type:text/csv");
        header("Content-Disposition:attachment;filename=".$filename);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        $data .= "ID,";
        $data .= "用户名,";
        $data .= "身份证,";
        $data .= "提现金额,";
        $data .= "剩余冻结资金,";
        $data .= "提现手续费,";
        $data .= "银行卡,";
        $data .= "持卡人,";
        $data .= "银行卡号,";
        $data .= "省份,";
        $data .= "城市,";
        $data .= "支行,";
        $data .= "备注,";
        $data .= "提现时间,\n";
        $i=1;
        if(!empty($list)){
            foreach($list as $k=>$v){
            	$data .= $v['id'].",";
            	$data .= $v['nickname'].",";
            	$data .= $v['id_card']."\t,";
            	$data .= $v['money'].",";
            	$data .= $v['blocked_fund'].",";
            	$data .= $v['handling_fee'].",";
            	$data .= $v['bank_name'].",";
            	$data .= $v['bank_realname'].",";
            	$data .= $v['bank_card']."\t,";
            	$data .= $v['pro_name'].",";
            	$data .= $v['city_name'].",";
            	$data .= $v['outlets_name'].",";
            	$data .= $v['remark'].",";           
                $data .=date('Y-m-d H:i:s',$v['add_time'])."\t,\n";
            }
            echo  @mb_convert_encoding($data, 'GB2312','utf-8');
            exit;
        }
    }
}
?>
